<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        <?php
        include "includes/ketnoi.php";
        include "includes/header.php";
        ?>
        <style type="text/css">
            .example{
                margin: 20px;
            }

        </style>
    </head>
    <body>
        <?php
        // put your code here
        include "includes/narbar.php";
        ?>

        <div class="container">
            <h1>Đăng kí</h1>
            <div class="row clearfix list-simple">
			
                <?php
                $sql1 = $conn->query("SELECT * FROM locations");
                $location = mysqli_fetch_all($sql1, MYSQLI_ASSOC);
                if (isset($_POST['submit'])) {
                    //  dung ham md5() de ma hoa password
                    $email = $_POST['email'];
                    $password = md5($_POST['password']);
                    $verify_password = md5($_POST['verify_password']);
                    $fname = $_POST['fname'];
                    $lname = $_POST['lname'];
                    $sex = $_POST['sex'];
                    $location_id = $_POST['id'];


                    // Kiem tra thong tin, neu co thong tin nao chua dien thi bao nhap lai
                    if (!$_POST['password'] || !$_POST['verify_password'] || !$email || !$fname || !$lname) {
                        print "Xin vui lòng nhập đầy đủ các thông tin. <a href='javascript:history.go(-1)'>Nhấp vào đây để quay trở lại</a>";
                        exit;
                    }
                    // Kiểm tra email nay co hop le ko
                    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                        print "Email này không hợp lệ. <a href='javascript:history.go(-1)'>Nhấp vào đây để quay trở lại</a>";
                        exit;
                    }

                    // Kiem tra mat khau co giong nhau khong
                    if ($password != $verify_password) {
                        print "Mật khẩu không giống nhau, bạn hãy nhập lại mật khẩu. <a href='javascript:history.go(-1)'>Nhấp vào đây để quay trở lại</a>";
                        exit;
                    }
                    $sql = "INSERT INTO users (group_id,location_id,email,password,fname,lname,sex) VALUES(2,$location_id,'$email','$password','$fname','$lname','$sex')";
                    if (!$conn->query($sql)) {
                        die('Error: ');
                    }
                    echo "Đăng kí thành công</br><a href='./dangnhap.php'>Nhấp vào đây để đăng nhập</a>";
                } else {
                    $_POST['email'] = $_POST['password'] = $_POST['verify_password'] = $_POST['fname'] = $_POST['lname'] = $_POST['sex'] = "";
                }
                ?>
            </div>

        </div>
		<form class="form-horizontal" action=""  role="form" method="post">
			<div class="modal-body">
				<div class="form-group">
			        <label class="col-md-3 col-xs-12  control-label">Họ tên</label>
			        <div class="col-md-4 col-xs-12">
			            <input type="text" class="form-control" name="fname" id="txtFirstname" value="" placeholder="Họ" tabindex="1">
			        </div>
			        <br class="visible-xs push-top-15">
			        <div class="col-md-4 col-xs-12">
			            <input type="text" class="form-control" name="lname" id="txtLastname" value="" placeholder="Tên" tabindex="2">
			        </div>
			    </div>
				<div class="form-group">
			        <label class="col-md-3  control-label" for="resumeTitle">Địa chỉ email</label>

			        <div class="col-md-8">
			            <input type="email" class="form-control" name="email" id="txtEmail" value="" tabindex="3">
			            <input type="hidden" name="chkEmail" value="0">
			        </div>
			    </div>
			    <div class="form-group">
			        <label class="col-md-3  control-label" for="resumeTitle">Mật khẩu</label>
			        <div class="col-md-4 col-xs-12">
			            <input type="password" class="form-control" name="password" id="txtPassword" maxlength="255" value="" tabindex="4" placeholder="Mật Khẩu">
			        </div>
					<br class="visible-xs push-top-15">
					<div class="col-md-4 col-xs-12">
			            <input type="password" class="form-control" name="verify_password" id="txtPassword" maxlength="255" value="" tabindex="4" placeholder="Xác Nhận Mật Khẩu">
			        </div>
			    </div>
				<div class="form-group">
			        <label class="col-md-3  control-label" for="resumeTitle">Giới Tính</label>
			        <div class="col-md-4 col-xs-12">
			            <input type="radio" name="sex" value ="Nam" checked="true"/>Nam
                        <input type="radio" name="sex" value="Nu" />Nữ</td>
			        </div>
			    </div>
				<div class="form-group">
			        <label class="col-md-3  control-label" for="resumeTitle">Nơi ở</label>
			        <div class="col-md-4 col-xs-12">
			           <select name ="id" class="form-control">
                        <?php
                            foreach ($location as $value) {
                            echo '<option value="' . $value['id'] . '">' . $value['name'] . '</option>';
                            }
                        ?> 
                        </select>
			        </div>
			    </div>
				<div class="form-group">
			        <label class="col-md-3  control-label" for="resumeTitle">Giới thiệu bản thân</label>
			        <div class="col-md-8">
			            <input type="text" class="form-control" name="gioithieu" value="" >
			        </div>
			    </div>
			</div>
			<div class="modal-footer">
				<input type="submit" name="submit" value="Đăng Kí" class="btn btn-primary"/>
				 <button type="button" class="btn btn-default" >Đóng</button> 
			</div>
			</form>
<?php
include "includes/footer.php";
?>
    </body>
</html>
